package com.zut.blocks.web.controller;

import com.zut.blocks.base.common.CommonResponse;
import com.zut.blocks.base.common.request.CourseRequest;
import com.zut.blocks.base.common.request.StudentRequest;
import com.zut.blocks.base.controller.GenericController;
import com.zut.blocks.base.domain.Course;
import com.zut.blocks.base.domain.Grade;
import com.zut.blocks.service.course.CourseService;
import com.zut.blocks.service.course.vo.Student;
import com.zut.blocks.service.course.vo.StudentCourseGroup;
import com.zut.blocks.service.grade.GradeService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lxd
 * @date 2020/12/28 10:51
 */
@RestController
@RequestMapping("/api/course")
public class CourseApi extends GenericController<Grade, Integer, GradeService> {

    @Resource
    CourseService courseService;

    @GetMapping
    public CommonResponse<List<Course>> find(CourseRequest courseRequest) {
        Page<Course> datasets = courseService.find(courseRequest);
        return CommonResponse.success(datasets.getContent(), datasets.getTotalElements());
    }

    @PostMapping("/add")
    public CommonResponse<Course> add(Course course) {
        return CommonResponse.success(courseService.add(course));
    }

    @PostMapping("/delete")
    public CommonResponse<Integer> delete(Integer id) {
        return CommonResponse.success(courseService.delete(id));
    }

    @GetMapping("/info")
    public CommonResponse<Course> course(Integer courseId) {
        return CommonResponse.success(courseService.findCourse(courseId));
    }

    @GetMapping("/student")
    public CommonResponse<List<Student>> findStudents(StudentRequest studentRequest) {
        return CommonResponse.success(courseService.findCourseStudent(studentRequest));
    }

    @GetMapping("/group")
    public CommonResponse<List<StudentCourseGroup>> group(Integer courseId) {
        return CommonResponse.success(courseService.findAllByCourseId(courseId));
    }
}
